﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Eventbrite.LinqProvider.Translators
{
    class KeywordsTranslator
    {

        private string _searchOperator = "+OR+";

        public string[] Keywords { get; private set; }

        public void Translate(Expression exp)
        {
            if (exp is ConstantExpression)
            {
                VisitConstant(exp as ConstantExpression);
            }
        }

        public static KeywordsTranslator GetAndSearchTranslator()
        {
            return new KeywordsTranslator()
            {
                _searchOperator = "+AND+"
            };
        }

        private void VisitConstant(ConstantExpression exp)
        {
            if (exp.Type == typeof(string[]))
            {
                Keywords = exp.Value as string[];
            }
        }

        public string KeywordParams()
        {
            StringBuilder sb = new StringBuilder();
            foreach (var key in Keywords)
            {
                if (sb.Length > 0)
                    sb.Append(_searchOperator);
                sb.Append(key);
            }
            return sb.ToString();
        }
    }
}
